package mylab.utils.common;


import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.experimental.UtilityClass;

@UtilityClass
public class OsUtil {

    @Getter
    @AllArgsConstructor
    public enum OS {
        //@formatter:off
        Windows             ("Windows"),
        Mac_OS              ("Mac OS"),
        Mac_OS_X            ("Mac OS X"),
        Android             ("Android"),
        Linux               ("Linux"),
        Unknown             ("Unknown"),

        //@formatter:on
        ;

        private final String description;

    }

    private final String OS_NAME = System.getProperty("os.name").toLowerCase();

    public boolean isAndroid() {
        try {
            Class.forName("android.os.Build");
            return true;
        } catch (ClassNotFoundException e) {
            return false;
        }
    }

    public boolean isLinux() {
        return OS_NAME.contains("linux");
    }

    public boolean isMacOS() {
        return OS_NAME.contains("mac") && OS_NAME.contains("os") && !OS_NAME.contains("x");
    }

    public boolean isMacOSX() {
        return OS_NAME.contains("mac") && OS_NAME.contains("os") && OS_NAME.contains("x");
    }

    public boolean isWindows() {
        return OS_NAME.contains("windows");
    }

    /**
     * 获取操作系统名字
     *
     * @return 操作系统名
     */
    public OS getOS() {
        OS os;

        if (isWindows()) {
            os = OS.Windows;
        } else if (isMacOS()) {
            os = OS.Mac_OS;
        } else if (isMacOSX()) {
            os = OS.Mac_OS_X;
        } else if (isAndroid()) {
            os = OS.Android;
        } else if (isLinux()) {
            os = OS.Linux;
        } else {
            os = OS.Unknown;
        }
        return os;
    }
}
